<?php

/**
 * Subclass for performing query and update operations on the 'sf_form_gui_question' table.
 *
 * 
 *
 * @package plugins.sfFormGuiPlugin.lib.model
 */ 
class PluginsfFormGuiPeer extends BasesfFormGuiPeer
{
    static function retrieveByRank($rank = 1)
    {
      $c = new Criteria;
      $c->add(self::RANK, $rank);
      return self::doSelectOne($c);
    }
     
    static function getAllByRank()
    {
      $c = new Criteria;
      $c->addAscendingOrderByColumn(self::RANK);
      return self::doSelect($c); 
    }
     
    static function getMaxRank()
    {
      $con = Propel::getConnection(self::DATABASE_NAME);
      $sql = 'SELECT MAX('.self::RANK.') AS max FROM '.self::TABLE_NAME; 
      $stmt = $con->prepareStatement($sql);
      $rs = $stmt->executeQuery();
     
      $rs->next();
      return $rs->getInt('max');
    }
    
    static function doSort($order)
    {
      $con = Propel::getConnection(self::DATABASE_NAME);
      try 
      {
        $con->begin();
     
        foreach ($order as $rank => $id) 
        {
          $item = sfFormGuiPeer::retrieveByPk($id);
          if($item->getRank() != $rank)
          {
            $item->setRank($rank);
            $item->save();
          }
        }
     
        $con->commit();
        return true;
      }
      catch (Exception $e)
      {
        $con->rollback();
        return false;
      }
    }

}
